package com.team10.hotel.service.impl;

import com.github.pagehelper.PageHelper;
import com.team10.hotel.domain.*;
import com.team10.hotel.mapper.*;
import com.team10.hotel.service.HotelService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

@Service
public class HotelServiceImpl implements HotelService {
    @Autowired
   private HotelMapper hotelMapper;
    @Autowired
    private CityMapper cityMapper;
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private BreakfastMapper breakfastMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private  HotelImageMapper hotelImageMapper;
    @Autowired
    private  RoomImageMapper roomImageMapper;
    @Autowired
    private  BreakfastImageMapper breakfastImageMapper;
    @Override
    public List<Hotel> listHotel() {
        return hotelMapper.selectByExample(new HotelExample()) ;
    }

    @Override
    public int updateHotel(Hotel hotel) {
        return hotelMapper.updateByPrimaryKeySelective(hotel);
    }

    @Override
    public Hotel findHotelByPrimaryKey(Integer id) {
        return hotelMapper.selectByPrimaryKey(id);
    }

    @Override
    public int insertHotel(Hotel hotel) {
        hotel.setHotelHot(0l);
        return hotelMapper.insert(hotel);
    }

    @Override
    public int deleteHotelByPrimaryKey(Integer id) {
        List<HotelImage>hotelImages=hotelImageMapper.selectImageByHotelId(id);
        for (HotelImage hotelImage:hotelImages
             ) {
            hotelImageMapper.deleteByPrimaryKey(hotelImage.getImageId());
        }
        List<Room> rooms=roomMapper.selectRoomByHotelId(id);
        for (Room room: rooms
             ) {
            List<RoomImage> roomImages=roomImageMapper.selectRoomImageByRoomId(room.getRoomId());
            for (RoomImage roomImage:roomImages
            ) {
                roomImageMapper.deleteByPrimaryKey(roomImage.getImageId());
            }
            roomMapper.deleteByPrimaryKey(room.getRoomId());
        }
        List<Breakfast>breakfasts=breakfastMapper.selectBreakfastByHotelId(id);
        for (Breakfast breakfast:breakfasts
             ) {
            List<BreakfastImage>breakfastImages=breakfastImageMapper.selectBreakfastImageByBreakfastId(breakfast.getBreakfastId());
            for (BreakfastImage breakfastImage:breakfastImages
                 ) {
                breakfastImageMapper.deleteByPrimaryKey(breakfastImage.getImageId());
            }
            breakfastMapper.deleteByPrimaryKey(breakfast.getBreakfastId());
        }
        return hotelMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<City> listCity() {
        return cityMapper.selectByExample(new CityExample());
    }

    @Override
    public int selectLastInsertId() {
        return hotelMapper.selectLastInsertId();
    }

    @Override
    public List<Hotel> listHotelAndImage(Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        return hotelMapper.selectAllAndImage();
    }

    @Override
    public List<Hotel> listElseHotelAndImage( String hotelLocation,Integer id) {
        return hotelMapper.selectElseHotelAndImage(id,hotelLocation);
    }

    @Override
    public List<Hotel> findAll() {
        return hotelMapper.selectAll();
    }


    @Override
    public List<Hotel> selectHotelByExample(String countryName,String provinceName,String cityName,String keyName,Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        HotelExample hotelExample=new HotelExample();
        HotelExample.Criteria criteria=hotelExample.createCriteria();
        if (!(countryName.isEmpty())){
            criteria.andHotelLocationLike('%'+countryName+'%');
        }
        if (!(provinceName.isEmpty())){
            criteria.andHotelLocationLike('%'+provinceName+'%');
        }
        if (!(cityName.isEmpty())){
            criteria.andHotelLocationLike('%'+cityName+'%');
        }
        if (!(keyName.isEmpty())) {
            criteria.andHotelNameLike('%'+keyName+'%');
        }
        return hotelMapper.selectByExample(hotelExample);
    }

    @Override
    public Hotel findHotelAndImageByPrimaryKey(Integer id) {
        return hotelMapper.findHotelAndImageByPrimaryKey(id);
    }

    @Override
    public Hotel findHotelExtByPrimaryKey(Integer id) {
        return hotelMapper.selectExtByPrimaryKey(id);
    }

    @Override
    public List<Hotel> listHotelAndImageHotSome(Integer amount) {
        return hotelMapper.selectAllAndImageHotSome(amount);
    }

    @Override
    public List<Hotel> listHotelAndImageNewBook(Integer amount) {
        return hotelMapper.selectAllAndImageNewBook(amount);
    }

    @Override
    public List<Hotel> selectHotelByNameAndLocation(String keyName, String keyLocation,Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        HotelExample example=new HotelExample();
        HotelExample.Criteria criteria=example.createCriteria();
        if(!keyName.isEmpty()){
            criteria.andHotelNameLike('%'+keyName+'%');
        }
        if(!keyLocation.isEmpty()){
            criteria.andHotelLocationLike('%'+keyLocation+'%');
        }
        return hotelMapper.selectByExample(example);
    }


}
